/* 当前年份 */
const currentYear = new Date().getFullYear();
const withData = param => {
  return param < 10 ? '0' + param : '' + param;
}
/* 
 根绝最大和最小值循环获取一个数组
 */
const getLoopArray = (min, max) => {
  var min = min || 0;
  var max = max || 1;
  var array = [];
  for (var i = min; i <= max; i++) {
    array.push(withData(i));
  }
  return array;
}
/* 
  获取指定年份和月份 对应的天数
 */
const getMonthDay = (year, month) => {
  var flag = year % 400 == 0 || (year % 4 == 0 && year % 100 != 0),
    array = null;
  switch (month) {
    case '01':
    case '03':
    case '05':
    case '07':
    case '08':
    case '10':
    case '12':
      array = getLoopArray(1, 31)
      break;
    case '04':
    case '06':
    case '09':
    case '11':
      array = getLoopArray(1, 30)
      break;
    case '02':
      array = flag ? getLoopArray(1, 29) : getLoopArray(1, 28)
      break;
    default:
      array = '月份格式不正确，请重新输入！'
  }
  return array;
}
/* 
 获取当前时间：年月日时分
 */
const getNewDateArry = (date) => {
  let newDate = null;
  if (date) {
    newDate = new Date(date)
  } else {
    newDate = new Date();
  }
  // var newDate = new Date();
  var year = withData(newDate.getFullYear()),
    mont = withData(newDate.getMonth() + 1),
    date = withData(newDate.getDate()),
    hour = withData(newDate.getHours()),
    minu = withData(newDate.getMinutes()),
    seco = withData(newDate.getSeconds());
  return [year, mont, date, hour, minu, seco];
}

Component({
  /** * 组件的属性列表 */
  properties: {
    //默认选中时间
    // currentDateTime: {
    // 	type: String,
    // 	value: '',
    // 	observer: 'updateValue',
    // },
    currentDate: {
      type: String,
      value: ''
    },
    //显示最小年份
    minYear: {
      type: Number,
      value: currentYear - 10,
      observer: 'updateValue',
    },
    //显示最大的年份
    maxYear: {
      type: Number,
      value: currentYear + 10,
      observer: 'updateValue',
    },
    //显示最小的月份
    minMouth: {
      type: Number,
      value: 1,
      observer: 'updateValue',
    },
    //显示最大的月份
    maxMouth: {
      type: Number,
      value: 12,
      observer: 'updateValue',
    },
    //
    minHour: {
      type: Number,
      value: 0,
      observer: 'updateValue',
    },
    maxHour: {
      type: Number,
      value: 23,
      observer: 'updateValue',
    },
    minMinute: {
      type: Number,
      value: 0,
      observer: 'updateValue',
    },
    maxMinute: {
      type: Number,
      value: 59,
      observer: 'updateValue',
    },
    minSecond: {
      type: Number,
      value: 0,
      observer: 'updateValue',
    },
    maxSecond: {
      type: Number,
      value: 59,
      observer: 'updateValue',
    },
  },
  observers: {
    'currentDate': function () {
      this.init();
    }
  },
  /** * 组件的初始数据 */
  data: {
    dateTimeArray: null,
    valueIndexArray: [0, 0, 0, 0, 0, 0],
    currentDateTimeStr: ''
  },
  lifetimes: {
    attached() {
      this.init();
    }
  },
  /** * 组件的方法列表 */
  methods: {
    init() {
      //选中时间
      var currentDateTime = []
      if (this.data.currentDate == '') {
        // currentDateTime = getNewDateArry()
        // this.triggerEvent("change", {
        //   currentDateTimeStr: currentDateTime[0] + '-' + currentDateTime[1] + '-' + currentDateTime[2] + ' ' + currentDateTime[3] + ':' + currentDateTime[4] + ':' + currentDateTime[5]
        // })
      } else {
        currentDateTime = getNewDateArry(this.data.currentDate)
      }
      this.setData({
        currentDateTime: currentDateTime,
        currentDateTimeStr: currentDateTime[0] + '-' + currentDateTime[1] + '-' + currentDateTime[2] + ' ' + currentDateTime[3] + ':' + currentDateTime[4] + ':' + currentDateTime[5]
      })
      this.initData()
    },
    updateValue: function (newVal, oldVal, changedPath) {},
    /* 初始化数据 */
    initData: function () {
      var dateTimeArray = [
          [],
          [],
          [],
          [],
          []
        ],
        valueIndexArray = [],
        currentDateTime = this.data.currentDateTime;
      //获取年
      dateTimeArray[0] = getLoopArray(this.data.minYear, this.data.maxYear)
      //获取月
      dateTimeArray[1] = getLoopArray(this.data.minMouth, this.data.maxMouth)
      //根据年月获取对应的天数
      dateTimeArray[2] = getMonthDay(currentDateTime[0], currentDateTime[1]);
      //获取小时
      dateTimeArray[3] = getLoopArray(this.data.minHour, this.data.maxHour);
      //获取分钟
      dateTimeArray[4] = getLoopArray(this.data.minMinute, this.data.maxMinute);
      dateTimeArray[5] = getLoopArray(this.data.minSecond, this.data.maxSecond);
      //获取默认选中时间的下标
      dateTimeArray.forEach((current, index) => {
        valueIndexArray.push(current.indexOf(currentDateTime[index]));
      });
      this.setData({
        dateTimeArray: dateTimeArray,
        valueIndexArray: valueIndexArray
      })
    },
    /* 列数据改变 */
    changeDateTimeColumn(e) {
      /* 判断是不是年或月改变 */
      if (e.detail.column <= 1) {
        var valueIndexArray = this.data.valueIndexArray,
          dateTimeArray = this.data.dateTimeArray;;
        valueIndexArray[e.detail.column] = e.detail.value;
        //重新获取日的数据
        dateTimeArray[2] = getMonthDay(dateTimeArray[0][valueIndexArray[0]], dateTimeArray[1][valueIndexArray[1]]);
        this.setData({
          dateTimeArray: dateTimeArray,
          valueIndexArray: valueIndexArray
        });
      }
    },
    /* 数据改变 */
    changeDateTime(e) {
      const currentDateTime = []
      currentDateTime[0] = this.data.dateTimeArray[0][e.detail.value[0]]
      currentDateTime[1] = this.data.dateTimeArray[1][e.detail.value[1]]
      currentDateTime[2] = this.data.dateTimeArray[2][e.detail.value[2]]
      currentDateTime[3] = this.data.dateTimeArray[3][e.detail.value[3]]
      currentDateTime[4] = this.data.dateTimeArray[4][e.detail.value[4]]
      currentDateTime[5] = this.data.dateTimeArray[5][e.detail.value[5]]
      this.setData({
        currentDateTime: currentDateTime,
        currentDateTimeStr: currentDateTime[0] + '-' + currentDateTime[1] + '-' + currentDateTime[2] + ' ' + currentDateTime[3] + ':' + currentDateTime[4] + ':' + currentDateTime[5]
      });
      this.triggerEvent("change", {
        currentDateTimeStr: currentDateTime[0] + '-' + currentDateTime[1] + '-' + currentDateTime[2] + ' ' + currentDateTime[3] + ':' + currentDateTime[4] + ':' + currentDateTime[5]
      })
    }
  }
})